/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.ctu.guiproject.server;

import cz.ctu.guiproject.server.manager.LayoutManager;
import cz.ctu.guiproject.server.shared.Event;
import guiproject.server.view.View;
import guiproject.server.widget.Button;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 *
 * @author tomas.buk
 */
public class Example implements EventObserver {
    
    private static final Logger logger = Logger.getLogger(Example.class.getName());
    
    private final UIServer server;
    private final LayoutManager layoutManager;

    @SuppressWarnings("LeakingThisInConstructor")
    public Example() {
        server = new UIServerString(6789);
        server.registerObserver(this);
        layoutManager = server.getLayoutManager();
        
        initGUI();
    }

    private void initGUI() {
        final View view = layoutManager.getView("karelButton");
        final Button button = (Button) view;
        
        final JFrame frame = new JFrame("Example");
        final JTextField txtField = new JTextField();
        txtField.setPreferredSize(new Dimension(120, 25));
        txtField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                button.setText(txtField.getText());
                txtField.setText("");
            }
        });
        frame.getContentPane().add(txtField);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

    @Override
    public void update(Event event) {
        logger.info(event.getEventId());
        logger.info(Arrays.toString(event.getKeys()));
        logger.info(Arrays.toString(event.getValues()));
    }
}
